<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.inc.php';
$link=connect();

if(isset($_COOKIE['youyou']['name']) && isset($_COOKIE['youyou']['pw'])){
	skip('index.php','error','你已经登录，请不要重复登录！');
}

//登录页面
if(isset($_GET['submit'])){

	//检查数据格式
	if(empty($_POST['name'])){
		skip('login.php', 'error', '用户名不得为空！');
	}
	else if(mb_strlen($_POST['name'])>12){
		skip('login.php', 'error', '用户名长度不要超过12个字符！');
	}
	if(empty($_POST['pw'])){
		skip('login.php', 'error', '密码不得为空！');
	}else if(mb_strlen($_POST['pw'])>18){
		skip('login.php', 'error', '密码长度不要超过12个字符！');
	}else if(mb_strlen($_POST['pw'])<6){
		skip('login.php', 'error', '密码长度不要低于6个字符！！');
	}
	if(empty($_POST['time']) || is_numeric($_POST['time']) || $_POST['time']>2592000){
		$_POST['time']=2592000;
	}

	//转义
	$_POST=escape($link,$_POST);

	if(strtolower($_POST['vcode'])!=strtolower($_SESSION['vcode'])){
		skip('login.php', 'error','验证码输入错误！');
	}
	$query="select * from userinfo where username='{$_POST['name']}' and password=md5('{$_POST['pw']}')";
	$result=execute($link, $query);
	if(mysqli_num_rows($result)==1){
		setcookie('youyou[name]',$_POST['name'],time()+$_POST['time']);
		setcookie('youyou[pw]',sha1(md5($_POST['pw'])),time()+$_POST['time']);
		skip('index.php','ok','登录成功！');
	}else{
		skip('login.php', 'error', '用户名或密码填写错误！');
	}
}

//侧边栏登录
if(isset($_GET['submit2'])){

	//检查数据格式
	if(empty($_POST['name'])){
		skip('login.php', 'error', '用户名不得为空！');
	}
	else if(mb_strlen($_POST['name'])>18){
		skip('login.php', 'error', '用户名长度不要超过18个字符！');
	}
	if(empty($_POST['pw'])){
		skip('login.php', 'error', '密码不得为空！');
	}else if(mb_strlen($_POST['pw'])>18){
		skip('login.php', 'error', '密码长度不要超过12个字符！');
	}else if(mb_strlen($_POST['pw'])<6){
		skip('login.php', 'error', '密码长度不要低于6个字符！！');
	}

	if(empty($_POST['time']) || is_numeric($_POST['time']) || $_POST['time']>2592000){
		$_POST['time']=2592000;
	}

	//转义
	$_POST=escape($link,$_POST);
	$query="select * from userinfo where username='{$_POST['name']}' and password=md5('{$_POST['pw']}')";
	$result=execute($link, $query);

	if(mysqli_num_rows($result)==1){
		if($_POST['auto']){
			setcookie('youyou[name]',$_POST['name'],time()+$_POST['auto']*86400);
			setcookie('youyou[pw]',sha1(md5($_POST['pw'])),time()+$_POST['auto']*86400);
			skip('index.php','ok','登录成功！');
		}else{
			setcookie('youyou[name]',$_POST['name'],time()+3600);
			setcookie('youyou[pw]',sha1(md5($_POST['pw'])),time()+3600);
			skip('index.php','ok','登录成功！');
		}

	}else{
		skip('index.php', 'error', '用户名或密码填写错误！');
	}
}

close($link);
?>

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
		<meta name="author" content="悠游网" />
		<meta name="copyright" content="悠游网络科技股份有限公司" />
		<title>欢迎登录_旅游_旅行_出国旅游_自驾游_周边游_旅游网站</title>
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<meta name='keywords' content='旅游,旅游网,旅行,出国旅游,自驾游,周边游,旅游网站,同程旅游' />
		<meta name='description' content='悠游网(YY.COM)是一家专业的一站式旅游参考平台，提供近万家景点门票、特价机票、出国旅游、周边游、自驾游及酒店预订服务；专业旅游线路服务、让您的旅行更安心！' />
		<link href="img/logo.gif" rel="shortcut icon"/>
		<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
		<link rel="stylesheet" type="text/css" href="css/style.css"/>
		<style type="text/css">
			@-webkit-keyframes In{
				0%{
					top:-80px;
					opacity: 0;
				}
				100%{
					top:0px;
					opacity: 1;
				}
			}
			@-moz-keyframes In{
				0%{
					top:-80px;
					opacity: 0;
				}
				100%{
					top:0px;
					opacity: 1;
				}
			}
			@keyframes In{
				0%{
					top:-80px;
					opacity: 0;
				}
				100%{
					top:0px;
					opacity: 1;
				}
			}
		</style>
	</head>
<body>
<div class="wrap">
	<div id="login" class="auto container-fluid">
		<div class="modal show" data-backdrop="static" style="animation: 1s In linear;"  id="myModa2" tabindex="-1">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal">
							<span>&times;</span>
						</button>
						<h2 class="modal-title">用户登录</h2>
					</div>
						<div class="modal-body">
							<div class="container-fluid">
								<div class="row">
									<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
										<p class="form-group">
											<label for="username"><span>*</span>用户名：</label>
											<input type="text" required="required" autocomplete="on" placeholder="请输入 6-12 位字符 " class="form-control" name="name" />
											<em class="em1"></em>
											<span class="prompt"><span></span><span></span></span>
										</p>
									</div>
									<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
										<p class="form-group">
											<label for="password1"><span>*</span>登录密码：</label>
											<input type="password" required="required" placeholder="请输入6-18位字符" class="form-control" name="pw" />
											<em class="em2"></em>
											<span class="prompt"><span></span><span></span></span>
										</p>
									</div>
									<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
										<p class="form-group">
											<label for="vis"><span>*</span>验证码：</label>
											<input type="text" class="form-control vis" name="vcode" />
											<a href="javascript:;" title="请点击刷新验证码"><img id="vcode" src="show_code.php" alt="请点击刷新验证码" border="0" align="absmiddle" onclick="this.src='show_code.php?rnd=' + Math.random();" /></a>
										</p>
									</div>
									<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 auto">
										<p class="form-group">
											<label for="time"><span>*</span>自动登录：</label>
											<select class="time" name="time">
												<option value="3600">1小时内</option>
												<option value="86400">1天内</option>
												<option value="259200">3天内</option>
												<option value="2592000">30天内</option>
											</select>
										</p>
									</div>
								</div>
							</div>
						</div>
						<div class="modal-footer">
							<input type="submit" name="submit" value="登录" class="btn btn-primary" />
							<div>没有悠游网账号?<a href="register.php">立即注册</a>  | <a href="index.php">悠游网首页</a></div>
						</div>
				</div>
			</div>
		</div>
	</div>
</div>
	<script src="js/jquery.min.js"></script>
	<script type="text/javascript">

		//表单验证
		var username=document.getElementsByName('name')[0];
		var pass=document.getElementsByName('pw')[0];
		var oformGroup=document.getElementsByClassName('form-group');
		var oPormpt=document.getElementsByClassName('prompt')[0].getElementsByTagName('span')[0];
		var oPormpt2=document.getElementsByClassName('prompt')[1].getElementsByTagName('span')[0];
		var oico=document.getElementsByClassName('prompt')[0].getElementsByTagName('span')[1];
		var oico2=document.getElementsByClassName('prompt')[1].getElementsByTagName('span')[1];
		oPormpt.classList.add('alert');
		oPormpt2.classList.add('alert');

		username.onblur=function(){
			checkUser();
		}

		pass.onblur=function(){
			checkPass();
		}

		document.onkeydown=function(ev){
			if(ev.keyCode==13){
				ajax();
			}
		}

		$('.btn').click(function(){

			ajax();

		});

		function ajax(){

			$.ajax({
				type:"post",
				url:"login.php?submit=1",
				data:"name="+username.value+"&pw="+pass.value+"&vcode="+$('.vis').val()+"&time="+$('.time').val(),
				async:true,
				success:function(data){

					document.writeln(data);

				}
			});

		}

		function checkUser(){
			if(username.value==""){
				oPormpt.classList.add('alert-warning');
				oico.classList.add('warning');
				oPormpt.innerHTML="用户名不能为空";
				oformGroup[0].classList.add('has-error');
				return false;
			}else if(username.value.length<4 || username.value.length>12){
				oPormpt.innerHTML="用户名必须是4-12位字符";
				oico.classList.add('warning');
				oPormpt.classList.add('alert-warning');
				oformGroup[0].classList.add('has-error');
				return false;
			}else{
				oformGroup[0].classList.remove('has-error');
				oformGroup[0].classList.add('has-success');
				oPormpt.classList.remove('alert-warning');
				oico.classList.remove('warning');
				oico.classList.add('success');
				oPormpt.innerHTML="4-12位，字母、数字";
			}

			for(var i=0;i<username.value.length;i++){
			    var charTest=username.value.toLowerCase().charAt(i);
				if( (!(charTest>='0' && charTest<='9')) &&  (!(charTest>='a' && charTest<='z'))  && (charTest!='_') ) {
					oformGroup[0].classList.add('has-error');
					oPormpt.innerHTML="用户名必须是4-12位字符";
				    return false;
			    }
			}
			return true;
		}

		function checkPass(){
			if(pass.value==""){
				oformGroup[1].classList.add('has-error');
				oPormpt2.classList.add('alert-warning');
				oico2.classList.add('warning');
				oPormpt2.innerHTML="密码不能为空";
				return false;
			}else if(pass.value.length<6 || pass.value.length>18){
				oformGroup[1].classList.add('has-error');
				oPormpt2.classList.add('alert-warning');
				oico2.classList.add('warning');
				oPormpt2.innerHTML="密码必须是6-18位字符";
				return false;
			}else{
				oformGroup[1].classList.remove('has-error');
				oPormpt2.classList.remove('alert-warning');
				oformGroup[1].classList.add('has-success');
				oico2.classList.remove('warning');
				oico2.classList.add('success');
				oPormpt2.innerHTML="6-18位，字母、数字";
			}
			return true;
		}

	</script>
	<script src="js/bootstrap.min.js"></script>
</body>
</html>
